﻿using System;
using System.Data;
using Framework.Library.Base.Base;
using Framework.Library.Base.DataAccess;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Config;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Data.DAL.JObject;

namespace Framework.Portal.Layouts.Admin.Components.Teenio.Category
{
    public partial class Categories : BaseControl
    {
        private readonly string MENUNAME = string.Empty;

        public Categories()
        {
        }

        public Categories(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            List<TeenioCategory> lstCategory = LoadZones();
            List<PortalMenu> lstMenu = ManagerFactory.PortalMenuManager.GetAll(Session.WorkingPortal().PORTALID);
            lstMenu = ReOrderMenu(lstMenu, lstCategory);
            if (lstMenu.Count > 0)
            {
                rptMenu.DataSource = lstMenu;
                rptMenu.DataBind();
            }
            drgType.Items.Add(new ListItem("Text", "0"));
            drgType.Items.Add(new ListItem("Audio", "1"));
            drgType.Items.Add(new ListItem("Text & Audio", "2"));
        }

        private List<PortalMenu> ReOrderMenu(List<PortalMenu> lstSource, List<TeenioCategory> lstCategory)
        {
            List<PortalMenu> lstMenu = new List<PortalMenu>();
            List<PortalMenu> tempMenu = lstSource.FindAll(l => (l.PARENTID == 0));
            if (tempMenu.Count > 0)
            {
                foreach (PortalMenu info in tempMenu)
                {
                    int index = lstCategory.FindIndex(x => x.MENUID == info.ID);
                    if (index > -1)
                        info.VISIBLE = 0;
                    lstMenu.Add(info);
                    ReOrderMenu(lstSource, lstMenu, info.ID, 0, lstCategory);
                }
            }
            return lstMenu;
        }

        private void ReOrderMenu(List<PortalMenu> lstSource, List<PortalMenu> lstDesc, int parentId, int level, List<TeenioCategory> lstCategory)
        {
            level++;
            List<PortalMenu> tempMenu = lstSource.FindAll(l => (l.PARENTID == parentId));
            if (tempMenu.Count > 0)
            {
                foreach (PortalMenu info in tempMenu)
                {
                    int index = lstCategory.FindIndex(x => x.MENUID == info.ID);
                    if (index > -1)
                        info.VISIBLE = 0;
                    string indent = string.Empty;
                    for (int i = 0; i < level; i++)
                    {
                        indent += "&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    info.NAME = indent + info.NAME;
                    lstDesc.Add(info);
                    ReOrderMenu(lstSource, lstDesc, info.ID, level, lstCategory);
                }
            }
        }

        protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (PortalMenu)e.Item.DataItem;
                var lblName = (Label)e.Item.FindControl("lblName");
                var lblSTT = (Label)e.Item.FindControl("lblSTT");

                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                lblName.Text = info.NAME;

                var btnEdit = (ImageButton)e.Item.FindControl("Label17");
                if (info.VISIBLE > 0)
                    btnEdit.CommandArgument = info.ID + "#" + info.NAME;
                else
                    btnEdit.Visible = false;
            }
        }

        public void rptMenu_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string cur = e.CommandArgument.ToString();
            if (!string.IsNullOrEmpty(cur))
            {
                switch (e.CommandName)
                {
                    case "edit":
                        txtName.Text = cur.Split('#')[1].Trim().Replace("&nbsp;", "");
                        hddMenuID.Value = cur.Split('#')[0].Trim();
                        break;
                    default:
                        break;
                }
            }
        }

        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (TeenioCategory)e.Item.DataItem;
                var lblName = (Label)e.Item.FindControl("lblName");
                var lblSTT = (Label)e.Item.FindControl("lblSTT");

                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                lblName.Text = info.ZONE_NAME;
            }
        }

        public void rptCategory_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int cur = Convert.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "edit":   
                    LoadInfo(cur);
                    break;
                case "delete":
                    ManagerFactory.DAOEntity.Delete(new TeenioCategory { ZONE_ID = cur });
                    lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    LoadZones();
                    GetDefault();
                    break;
                default:
                    break;
            }
        }

        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            try
            {
                ManagerFactory.DAOEntity.Insert(GetInfo());
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
            LoadZones();
            GetDefault();
        }

        protected void cmdEmpty_Click(object sender, EventArgs e)
        {
            GetDefault();
        }

        protected void cmdDel_Click(object sender, EventArgs e)
        {
            try
            {
                ManagerFactory.DAOEntity.Delete(new TeenioCategory { ZONE_ID = ConvertUtility.ToInt32(lblID.Text) });
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
            LoadZones();
            GetDefault();
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            TeenioCategory info = GetInfo();            
            try
            {
                int zoneID;
                int.TryParse(lblID.Text, out zoneID);
                info.ZONE_ID = zoneID;
                ManagerFactory.DAOEntity.Update(info);
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
            LoadZones();
            GetDefault();
        }

        private void GetDefault()
        {
            lblID.Text = string.Empty;
            txtName.Text = string.Empty;
            fpkImages.Text = string.Empty;
            drgType.SelectedIndex = -1;
        }

        private TeenioCategory GetInfo()
        {
            var info = new TeenioCategory();
            info.ZONE_NAME = txtName.Text.Trim();
            info.ZONE_PRIORITY = 0;
            info.ZONE_IMAGES = fpkImages.Text;
            int outValue;
            int.TryParse(hddMenuID.Value, out outValue);
            info.MENUID = outValue;
            int.TryParse(drgType.SelectedValue, out outValue);
            info.ZONETYPE = outValue;
            return info;
        }

        protected List<TeenioCategory> LoadZones()
        {
            List<TeenioCategory> lstCategory = ManagerFactory.TeenioCategoryManager.GetCategoriesByPortal(Session.WorkingPortal().PORTALID);
            if (lstCategory.Count > 0)
            {
                rptCategory.DataSource = lstCategory;
                rptCategory.DataBind();
                return lstCategory;
            }
            return new List<TeenioCategory>();
        }

        private void LoadInfo(int zoneID)
        {
            TeenioCategory info = ManagerFactory.DAOEntity.GetInfo(new TeenioCategory() { ZONE_ID = zoneID });
            if (info == null)
                return;
            lblID.Text = info.ZONE_ID.ToString();
            txtName.Text = info.ZONE_NAME;
            fpkImages.Text = info.ZONE_IMAGES;
            hddMenuID.Value = info.MENUID.ToString();
            drgType.SelectedValue = info.ZONETYPE.ToString();
        }
    }
}